package com.spukmk3me.animatorpack.pack0;

import com.spukmk3me.math.Matrix4f;
import com.spukmk3me.scene.Animator;
import com.spukmk3me.scene.Node;

public class RotationAnimator extends Animator
{
    public RotationAnimator()
    {
        m_running = false;
    }
    
    protected void animate( int time )
    {
        if ( !m_running )
            return;
        
        m_currentAngle += ((float)time / 0x00010000) * m_rotSpeed;
        
        while ( m_currentAngle >= Math.PI * 2 )
            m_currentAngle -= Math.PI * 2;
        
        while ( m_currentAngle <= -Math.PI * 2 )
            m_currentAngle += Math.PI * 2;
        
        applyToNode();
    }

    protected void onNodePrepareToChange( Node newNode )
    {
        
    }

    protected void onNodeChanged()
    {
        if ( getNode() == null )
            m_running = false;
        else
        {
            applyToNode();
            m_running = true;
        }
    }
    
    public boolean isAnimating()
    {
        if ( getNode() == null )
            return false;
        
        return m_running;
    }
    
    public void setupRotation( float centerX, float centerY,
        float startAngle, float rotationSpeed, float translateX, float translateY )
    {
        m_centerX = -centerX;
        m_centerY = -centerY;
        m_currentAngle = startAngle;
        m_rotSpeed = rotationSpeed;
        m_tx = translateX + centerX;
        m_ty = translateY + centerY;
        m_running = false;
        
        if ( getNode() != null )
        {
            applyToNode();
            m_running = true;
        }
    }
    
    private void applyToNode()
    {
        Node node = getNode();
        
        node.reset();
        node.t( m_centerX, m_centerY, 0f );
        node.r( 0f, 0f, 1f, m_currentAngle );
        node.t( m_tx, m_ty, 0f );
    }
    
    private float   m_centerX, m_centerY, m_currentAngle, m_rotSpeed, m_tx, m_ty;
    private boolean m_running;
}
